from student import Student


class FileManager:

    def __init__(self, file_name='data.txt'):
        self.file_name = file_name

    def read(self):
        stu_list = []

        f = open(self.file_name, 'r')
        for line in f:
            line = line.strip()
            if line == '':
                continue

            stu = Student.init_from_line(line)
            stu_list.append(stu)

        f.close()
        return stu_list

    def write_list(self, stu_list):
        f = open(self.file_name, 'w')
        # f.write('学号 姓名 数学成绩 英语成绩 语文成绩 总分')

        for item in stu_list:
            f.write(item.get_obj_str())
            f.write('\n')

        f.close()

    def append_student(self, student):
        line = f'{student.id} {student.name} {student.math_score} {student.english_score} {student.chinese_score} {student.sum_score}'
        self.append(line)

    def append(self, content):
        f = open(self.file_name, 'a')
        f.write('\n')
        f.write(content)
        f.close()


if __name__ == '__main__':
    file_manager = FileManager('data.txt')
    student_list = file_manager.read()
    for s in student_list:
        print(s.id, s.name)
